__author__ = """waen <wangjian@21epub.com>"""
__docformat__ = 'restructuredtext'


from AccessControl import ClassSecurityInfo
from zope.interface import implements

from Products.ATContentTypes.content.base import registerATCT

from Products.ePubBook.config import PROJECTNAME
from Products.ePubBook.interfaces import IBook
from Products.ATContentTypes.content.folder import ATFolder
from Products.ePubBook import ePubBookMessageFactory as _

from Products.Archetypes import atapi
from Products.ATContentTypes.content.folder import ATFolderSchema
from Products.ATContentTypes.content.schemata import finalizeATCTSchema
from archetypes.referencebrowserwidget import ReferenceBrowserWidget

from Products.Archetypes.public import DisplayList

# Schema definition
BookSchema = ATFolderSchema.copy() + atapi.Schema((
    atapi.StringField('isbn',
                    searchable = True,
                    widget = atapi.StringWidget(
                        label = _(u'label_info_isbn',default=u'ISBN'),
                        description = _(u'help_info_isbn',default=u''),
                        macro='my_string_widget',
                    )
    ),
    atapi.StringField('press',
                    searchable = True,
                    widget = atapi.StringWidget(
                        label = _(u'label_info_press',default=u'Press'),
                        description = _(u'help_info_press',default=u''),
                        macro='my_string_widget',
                    )
    ),
    atapi.ImageField('cover',
                    required = True,
                    default_output_type='image/jpeg',
                    allowable_content_types=('image/*',),
                    sizes= {
                            'large'    : (535, 535),                            
                            'mini'    : (180, 180),                            
                    },
                    widget = atapi.ImageWidget(
                        label = _(u'label_info_cover',default = u'Cover'),
                        description = _(u'help_info_cover',default=u''),
                        macro = "my_image_widget"
                        
                    )                   
    ),
    atapi.ReferenceField('author',
                         relationship = 'writedBy',
                         languageIndependent = False,
                         widget = ReferenceBrowserWidget(
                                                allow_search = True,
                                                allow_browse = True,
                                                show_indexes = False,
                                                force_close_on_insert = True,
                                                label = _(u'label_author', default=u'Author'),
                                                description = ''
                                                )
                          ),
    atapi.StringField(
        'previewnum',
        vocabulary = '_getPreviewNum',
        enforceVocabulary = 1,
        widget=atapi.SelectionWidget(
            label=_(u"label_info_previewnum",default=u'PreviewNum'),
            macro='my_selection_widget',
        ),
        default='5'
    ),
    
    atapi.StringField(
        'keywords',
        widget=atapi.StringWidget(
            label=_(u"label_info_keywords",default=u'Keywords'),
            macro='my_string_widget',                       
        ),
    ),
    atapi.StringField(
        'price',
        vocabulary = '_getPrice',
        enforceVocabulary = 1,
        widget=atapi.SelectionWidget(
            label=_(u"label_info_price",default=u'Price'),
            macro="my_selection_widget"
        ),
    ),
    atapi.BooleanField(
        'isinsite',
        mode="rw",
        default=False,
        widget=atapi.BooleanWidget(
            label=_(u"label_info_isinsite",default=u'publish to site'),
        ),
    ),
))
#BookSchema['description'].required = True
BookSchema['title'].widget.macro='my_string_widget'
BookSchema['description'].widget.macro = 'my_textarea_widget'
finalizeATCTSchema(BookSchema)

class EpubBook(ATFolder):
    """A book,which contains many pages """
    portal_type      = 'EpubBook'
    archetype_name   = 'EpubBook'
    _atct_newTypeFor = {'portal_type' : 'CMF Book', 'meta_type' : 'Epub Book'}

    implements(IBook)
    schema = BookSchema
    def _getPreviewNum(self):
        return DisplayList((               
            ('5', _(u'preview5',default=u'Preview 5 pages')),
            ('10', _(u'preview10',default=u'Preview 10 pages')),
            ('all', _(u'previewall',default=u'Preview All'))
        ))
        
    def _getPrice(self):
        return DisplayList((
            ('free', _(u'tier0',default=u'free')),
            ('$0.99', _(u'tier1',default=u'$0.99')),
            ('$1.99', _(u'tier2',default=u'$1.99')),
            ('$2.99', _(u'tier3',default=u'$2.99')),
            ('$3.99', _(u'tier3',default=u'$3.99')),
            ('$4.99', _(u'tier4',default=u'$4.99')),
            ('$5.99', _(u'tier5',default=u'$5.99')),
            ('$6.99', _(u'tier6',default=u'$6.99')),
            ('$7.99', _(u'tier7',default=u'$7.99')),
            ('$8.99', _(u'tier8',default=u'$8.99')),
            ('$9.99', _(u'tier9',default=u'$9.99'))
        ))
registerATCT(EpubBook,PROJECTNAME)